<template>
  <div class="advanced-settings">
    <h3 class="section-title">{{ $t('settings.advancedSettings') }}</h3>
    
    <!-- 单词数量设置 -->
    <div class="setting-group">
      <h4 class="setting-title">{{ $t('settings.wordCountSetting') }}</h4>
      <div class="setting-controls">
        <label class="setting-label">{{ $t('settings.wordCountPerLevel') }}</label>
        <div class="slider-container">
          <input 
            type="range" 
            min="5" 
            max="30" 
            step="5" 
            v-model.number="wordCount" 
            @change="updateWordCount"
          />
          <div class="slider-value">{{ wordCount }}</div>
        </div>
      </div>
    </div>
    
    <!-- 当前学习类型的阅读设置 -->
    <div class="setting-group">
      <h4 class="setting-title">{{ $t('settings.readingSettings') }}</h4>
      <div class="setting-controls">
        <label class="setting-label">{{ $t('settings.articleLength') }}</label>
        <div class="slider-container">
          <input 
            type="range" 
            min="100" 
            max="1000" 
            step="100" 
            v-model.number="readingLength" 
            @change="updateReadingLength"
          />
          <div class="slider-value">{{ readingLength }}</div>
        </div>
      </div>
      
      <div class="setting-controls">
        <label class="setting-label">{{ $t('settings.questionCount') }}</label>
        <div class="slider-container">
          <input 
            type="range" 
            min="1" 
            max="10" 
            step="1" 
            v-model.number="readingQuestionCount" 
            @change="updateReadingQuestionCount"
          />
          <div class="slider-value">{{ readingQuestionCount }}</div>
        </div>
      </div>
    </div>
    
    <!-- 当前学习类型的听力设置 -->
    <div class="setting-group">
      <h4 class="setting-title">{{ $t('settings.listeningSettings') }}</h4>
      <div class="setting-controls">
        <label class="setting-label">{{ $t('settings.audioLength') }}</label>
        <div class="slider-container">
          <input 
            type="range" 
            min="100" 
            max="600" 
            step="50" 
            v-model.number="listeningLength" 
            @change="updateListeningLength"
          />
          <div class="slider-value">{{ listeningLength }}</div>
        </div>
      </div>
      
      <div class="setting-controls">
        <label class="setting-label">{{ $t('settings.questionCount') }}</label>
        <div class="slider-container">
          <input 
            type="range" 
            min="1" 
            max="10" 
            step="1" 
            v-model.number="listeningQuestionCount" 
            @change="updateListeningQuestionCount"
          />
          <div class="slider-value">{{ listeningQuestionCount }}</div>
        </div>
      </div>
    </div>
    
    <!-- 引导设置 -->
    <div class="setting-group">
      <h4 class="setting-title">{{ $t('settings.otherSettings') }}</h4>
      <div class="setting-controls checkbox-control">
        <label class="setting-label">{{ $t('settings.showGuide') }}</label>
        <div class="toggle-switch">
          <input 
            type="checkbox" 
            id="guide-toggle" 
            v-model="isGuide" 
            @change="updateIsGuide"
          />
          <label for="guide-toggle"></label>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch, onMounted } from 'vue';
import { useSettingsStore } from '@/stores/settings';
import { useI18n } from 'vue-i18n';

const settingsStore = useSettingsStore();
const { t } = useI18n();

// 本地响应式状态
const wordCount = ref(10);
const readingLength = ref(500);
const readingQuestionCount = ref(4);
const listeningLength = ref(300);
const listeningQuestionCount = ref(4);
const isGuide = ref(true);

// 计算属性，根据当前学习类型返回对应的设置字段名
const readingLengthField = computed(() => 
  settingsStore.studyType === 1 ? 'ieltsReadingLength' : 'toeflReadingLength'
);

const readingQuestionCountField = computed(() => 
  settingsStore.studyType === 1 ? 'ieltsReadingQuestionCount' : 'toeflReadingQuestionCount'
);

const listeningLengthField = computed(() => 
  settingsStore.studyType === 1 ? 'ieltsListeningLength' : 'toeflListeningLength'
);

const listeningQuestionCountField = computed(() => 
  settingsStore.studyType === 1 ? 'ieltsListeningQuestionCount' : 'toeflListeningQuestionCount'
);

// 监听store中的设置变化，更新本地状态
watch(() => settingsStore.wordCount, (newValue) => {
  wordCount.value = newValue;
});

watch(() => settingsStore.studyType, () => {
  // 当学习类型变化时，更新本地状态
  updateLocalState();
});

// 初始化本地状态
const updateLocalState = () => {
  wordCount.value = settingsStore.wordCount;
  isGuide.value = settingsStore.isGuide;
  
  // 根据当前学习类型设置对应的阅读和听力设置
  if (settingsStore.studyType === 1) {
    readingLength.value = settingsStore.ieltsReadingLength;
    readingQuestionCount.value = settingsStore.ieltsReadingQuestionCount;
    listeningLength.value = settingsStore.ieltsListeningLength;
    listeningQuestionCount.value = settingsStore.ieltsListeningQuestionCount;
  } else {
    readingLength.value = settingsStore.toeflReadingLength;
    readingQuestionCount.value = settingsStore.toeflReadingQuestionCount;
    listeningLength.value = settingsStore.toeflListeningLength;
    listeningQuestionCount.value = settingsStore.toeflListeningQuestionCount;
  }
};

// 更新设置的方法
const updateWordCount = () => {
  settingsStore.updateUserSettings({ wordCount: wordCount.value });
};

const updateReadingLength = () => {
  const settings = {};
  settings[readingLengthField.value] = readingLength.value;
  settingsStore.updateUserSettings(settings);
};

const updateReadingQuestionCount = () => {
  const settings = {};
  settings[readingQuestionCountField.value] = readingQuestionCount.value;
  settingsStore.updateUserSettings(settings);
};

const updateListeningLength = () => {
  const settings = {};
  settings[listeningLengthField.value] = listeningLength.value;
  settingsStore.updateUserSettings(settings);
};

const updateListeningQuestionCount = () => {
  const settings = {};
  settings[listeningQuestionCountField.value] = listeningQuestionCount.value;
  settingsStore.updateUserSettings(settings);
};

const updateIsGuide = () => {
  settingsStore.updateUserSettings({ isGuide: isGuide.value });
};

onMounted(() => {
  // 初始化设置
  if (!settingsStore.initialized) {
    settingsStore.fetchSettings().then(() => {
      updateLocalState();
    });
  } else {
    updateLocalState();
  }
});
</script>

<style scoped>
.advanced-settings {
  margin-top: 2rem;
}

.section-title {
  font-size: 1.5rem;
  margin-bottom: 1rem;
  color: #fff;
  font-weight: 600;
}

.setting-group {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 0.5rem;
  padding: 1.5rem;
  margin-bottom: 1.5rem;
}

.setting-title {
  font-size: 1.2rem;
  margin-top: 0;
  margin-bottom: 1rem;
  color: #fff;
}

.setting-controls {
  display: flex;
  align-items: center;
  margin-bottom: 1rem;
}

.setting-controls:last-child {
  margin-bottom: 0;
}

.setting-label {
  flex: 0 0 30%;
  color: rgba(255, 255, 255, 0.8);
  font-size: 0.9rem;
}

.slider-container {
  flex: 1;
  display: flex;
  align-items: center;
}

input[type="range"] {
  flex: 1;
  -webkit-appearance: none;
  width: 100%;
  height: 6px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.2);
  outline: none;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #4A99E9;
  cursor: pointer;
}

input[type="range"]::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #4A99E9;
  cursor: pointer;
  border: none;
}

.slider-value {
  margin-left: 1rem;
  min-width: 2.5rem;
  text-align: center;
  background: rgba(255, 255, 255, 0.1);
  padding: 0.25rem 0.5rem;
  border-radius: 0.25rem;
  font-size: 0.9rem;
}

.checkbox-control {
  display: flex;
  align-items: center;
}

.toggle-switch {
  position: relative;
  display: inline-block;
  width: 50px;
  height: 24px;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-switch label {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(255, 255, 255, 0.2);
  transition: .4s;
  border-radius: 24px;
}

.toggle-switch label:before {
  position: absolute;
  content: "";
  height: 16px;
  width: 16px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  transition: .4s;
  border-radius: 50%;
}

.toggle-switch input:checked + label {
  background-color: #4A99E9;
}

.toggle-switch input:checked + label:before {
  transform: translateX(26px);
}

@media (max-width: 768px) {
  .setting-controls {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .setting-label {
    margin-bottom: 0.5rem;
  }
  
  .slider-container {
    width: 100%;
  }
}
</style> 